/*
**弹出层
*/
popup = {
	cover:null,
	cfm:null,
	load:null,
	alt:null,
	currentPop:null,
	esc:false, //设置是否能用ESC键退出
	showCover:function(){
		if(this.cover) 
		this.cover.height($(document).height()).show();//动态算出当前页面的高度，以防dom高度在页面渲染后有增加
		else{
			$("body").append("<div id='popupDiv'></div>");
			this.cover = $("#popupDiv").height($(document).height());
		}
		return true;
	},
	close:function(){
		this.cover.hide();
		this.currentPop.hide();
	},
	confirm:function(html,fn){ //确认框
		var cfm = this.cfm;
		this.esc = false;
		if(this.showCover()){
			if(!html) html = "undefined";
			if(!cfm){
				cfm = $("<div id='popupConfirm'>")
				.append($("<div class='popup_bg1'><span class='popup_title'>请确认</span><div class='popup_close' onclick='popup.close();'></div></div>"))
				.append($("<div class='cfm_m'><div class='cfm_r'><p>" + html + "</p></div></div>"))
				.append($("<div class='cfm_b'><input type='button' id='cfmRightBtn' value='取消' onclick='popup.close();'><input type='button' id='cfmLeftBtn' value='确认'></div>"))
				.appendTo($("body"));
			}
			else cfm.find(".cfm_r p").html(html);
			this.showPop(cfm);

			// 拖动
			var isDown = false;
			cfm.find(".popup_bg1").bind("mouseout mouseup",function(){
				isDown = false;
				$(this).unbind("mousemove");
			}).mousedown(function(e){
				isDown = true;
				e.preventDefault();
				var	s_x = $(window).scrollLeft(),
					s_y = $(window).scrollTop(),
					deltaX = e.clientX + s_x - cfm.offset().left,
					deltay = e.clientY + s_y - cfm.offset().top; 

				$(this).bind("mousemove", function(evt){
					if(isDown){
						cfm.offset({"left" : evt.clientX + s_x - deltaX,"top" : evt.clientY + s_y - deltay});
					}
				});
			});

			cfm.find("#cfmLeftBtn").unbind("click")//清除掉之前的事件绑定
			.bind("click",function(){
				popup.close();
				if(typeof fn === "function") fn();
			});
		}
	},
	alert:function(html, fn){
		this.esc = false;
		if(this.showCover()){
			if(!html) html = "undefined";
			if(!this.alt){
				this.alt = $("<div>",{"id":"popupAlert"})
				.append($("<div>").html("<span class='popup_title'>消息</span>").addClass("popup_bg1").append($("<div>").addClass("popup_close").click(function(){popup.close();})))
				.append($("<div>").addClass("cfm_m").append($("<div>").addClass("cfm_r").html("<p>" + html + "</p>")))
				.append($("<div>").addClass("cfm_b").append("<input type='button' id='cfmLeftBtn' value='确认'>"))
				.appendTo("body");
			}
			else this.alt.find(".cfm_r p").html(html);
			this.showPop(this.alt);
			this.alt.find('#cfmLeftBtn').unbind("click")//清除掉之前的事件绑定
			.bind("click", function(){
				popup.close();
				if(typeof fn === "function") fn();
			});
		}
	},
	loading:function(){
		this.esc = false;
		if(this.showCover()){
			if(!this.load){
				this.load = $("<div>",{"id":"popupLoading"})
				.append($("<div>",{"class":"load_img"}))
				.append($("<div>",{"class":"load_msg","text":"请稍后..."}))
				.appendTo("body");
			}
			this.showPop(this.load);
		}
	},
	iframe:function(url){
		this.esc = false;
		if(this.showCover()){
			if(!this.frm){
				this.frm = $("<div>",{"id":"popupNews"})
				.append($("<div>",{"id":"popupBg"})
					.append($("<div>").addClass("popup_bg1").append($("<div>").addClass("popup_close").click(function(){popup.close();})))
					.append($("<iframe>",{"id":"popupIframe","frameborder":"no","src":url})))
				.appendTo($("body"));
			}
			else this.frm.children("#popupIframe").attr("src", url);
			this.showPop(this.frm);
			this.esc = true;
		}
	},
	showPop:function(obj){
		if(this.currentPop)	this.currentPop.hide();//确保弹出框显现的唯一性
		this.currentPop = obj;
		this.position(obj.show());
	},
	position:function(obj){//计算位置，使弹出框上下左右居中
		var w = $(window), h = w.height(), obj_h = obj.height(), top = obj.css("position") === "absolute" ? w.scrollTop() : 0;
		if(h > obj_h) top += (h - obj_h) / 2;
		obj.css({"top":top, "left":(w.width() - obj.width()) / 2});
	}
}

// esc键关闭弹出框
$(document).keydown(function(e){
	e = e || event;
	if(e.keyCode === 27 && popup.esc) popup.close();
});